<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="obj.keyword"
          style="margin-bottom: 10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="hInput"
        />
        <el-tree
          ref="tree"
          node-key="id"
          default-expand-all
          :data="depts"
          :props="defaultProps"
          :expand-on-click-node="false"
          highlight-currenty
          check-strictly
          @current-change="hclick"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button
            size="mini"
            @click="showExcelDialog = true"
          >excel导入</el-button>
          <!-- v-if="$store.state.user.userInfo.roles.points.includes('excel_export')" -->
          <el-button v-allow="'excel_export'" size="mini" @click="hExport">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <img
                v-if="row.staffPhoto"
                style="width: 30px"
                :src="row.staffPhoto"
                alt=""
              >
              <span v-else class="username">
                {{ row.username.charAt(0) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="280px">
            <template v-slot="{ row }">
              {{ row.id }}
              <el-button
                size="mini"
                type="text"
                @click="$router.push('/employee/detail/' + row.id)"
              >查看</el-button>
              <el-button size="mini" type="text" @click="hAssignRole(row.id)">角色</el-button>
              <el-button size="mini" type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" style="height: 60px" align="middle" justify="end">
          <!-- 放置分页组件
            @current-change="hCurrentChange"
            @size-change="hSizeChange"
          -->
          <el-pagination
            :current-page="obj.page"
            :page-sizes="[3, 5, 7]"
            :background="true"
            layout="prev, pager, next, sizes"
            :total="total"
            :page-size="obj.pagesize"
            @current-change="hCurrentChange"
          />
        </el-row>
      </div>
    </div>
    <ImportExcel
      :show-excel-dialog.sync="showExcelDialog"
      @updataExcel="loadEmployment"
    />
    <AssignRole ref="refsRoles" :cur-id="curId" :show-role-dialog.sync="showRoleDialog" />
  </div>
</template>

<script>
import ImportExcel from './importexcel/import-excel.vue'
import { arr2tree } from '@/utils'
import { getDepartment } from '@/api/department'
import { getUser, exportEmployee } from '@/api/employment'
import FileSaver from 'file-saver'
import AssignRole from './importexcel/assignRole.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel,
    AssignRole
  },
  data() {
    return {
      curId: -1,
      showRoleDialog: false,
      showExcelDialog: false,
      total: 0,
      list: [],
      obj: {
        page: 1,
        pagesize: 10,
        keyword: null,
        departmentId: null
      },
      id: '', //  父传子
      showDialog: false,
      isEdit: false,
      depts: [
        // {
        //   name: '传智教育',
        //   children: [
        //     { name: '总裁办' },
        //     { name: '行政部' },
        //     { name: '财务部' }
        //   ]
        // }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    // 渲染封装
    this.dashboardEmployee()
  },
  methods: {
    hAssignRole(id) {
      //  开启弹框
      this.showRoleDialog = true
      //  需要id
      this.curId = id
      // 父调用子组件请求
      this.$nextTick(() => {
        this.$refs.refsRoles.getRolesById()
      })
    },
    // 发送导出员工excel请求
    async hExport() {
      const res = await exportEmployee()
      console.log(res)
      FileSaver.saveAs(res, '员工信息.xls')
    },
    hCurrentChange(page) {
      console.log(page)
      this.obj.page = page
      // 重新获取员工列表
      this.loadEmployment()
    },
    //  发送 获取 部门列表
    async dashboardEmployee() {
      const res = await getDepartment()
      // console.log(res)
      this.depts = arr2tree(res.data, 0)
      const data = this.depts
      console.log(data[0].id)
      const id = data[0].id
      this.obj.departmentId = id
      // console.log(res.data)
      this.$nextTick(() => {
        this.$refs.tree.setCurrentKey(id)
      })
      // 重新调用
      this.loadEmployment()
    },
    // 发送 获取 员工列表
    async loadEmployment() {
      const res = await getUser(this.obj)
      console.log(res)
      this.list = res.data.rows
      this.total = res.data.total
    },
    hclick(data, node) {
      console.log(data, node)
      this.obj.departmentId = data.id
      this.loadEmployment()
    },
    hInput(value) {
      console.log(value)
      // 回到第一页
      this.obj.page = 1
      // 重新渲染
      this.loadEmployment()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>
